import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

import 'PhotoAlbumView.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // 获取Android版本号
  Future<int> _getAndroidVersion() async {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
    return androidInfo.version.sdkInt;
  }

  // 提示框
  void _showDialog(BuildContext context, String title, String content) {
    // 显示弹框的函数
    showDialog(
      context: context,
      builder: (BuildContext context) {
        // 返回一个AlertDialog对象
        return AlertDialog(
          title: new Text(title),
          content: new Text(content),
          actions: <Widget>[
            TextButton(
              child: Text('前往'),
              onPressed: () {
                Navigator.of(context).pop();
                openAppSettings();
              },
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: Container(
        width: 250,
        height: 50,
        child: Builder(
            builder: (BuildContext context) {
              return TextButton(
                onPressed: () async {

                  // 判断Android版本获取不同权限
                  int version = await _getAndroidVersion();
                  print("~~~ Android version: $version");

                  if (version >= 33) {

                    // 适用于 Android 13 及以上版本，请求精细的媒体访问权限
                    Map<Permission, PermissionStatus> statuses = await [
                      Permission.photos,
                      Permission.videos,
                    ].request();
                    print("~~~ statuses: $statuses");
                    if ((statuses[Permission.photos] != PermissionStatus.granted && statuses[Permission.photos] != PermissionStatus.limited) ||
                        (statuses[Permission.videos] != PermissionStatus.granted && statuses[Permission.videos] != PermissionStatus.limited)) {
                      _showDialog(context, "请求存储权限", "需要存储权限来访问和保存图片及视频;");
                      return;
                    }

                  } else if (version >= 29) {

                    // 适用于 Android 10 至 Android 12，请求读取外部存储权限，而不是存储（即写权限）
                    PermissionStatus status = await Permission.storage.request();
                    print("~~~ status: $status");
                    if (status != PermissionStatus.granted) {
                      _showDialog(context, "请求存储权限", "需要存储权限来访问和保存图片及视频;");
                      return;
                    }

                  } else {

                    // 适用于 Android 9 及以下版本，请求旧的存储权限
                    PermissionStatus status = await Permission.storage.request();
                    print("~~~ status: $status");
                    if (status != PermissionStatus.granted) {
                      _showDialog(context, "请求存储权限", "需要存储权限来访问和保存图片及视频;");
                      return;
                    }

                  }

                  Navigator.push(context, MaterialPageRoute(builder: (context) => PhotoAlbumView()));

                },
                style: TextButton.styleFrom(
                  foregroundColor: Colors.white, // 文本颜色
                  backgroundColor: Colors.transparent, // 使按钮背景透明
                  padding: EdgeInsets.all(0), // 移除内边距
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(30),
                  ),
                ),
                child: const Text(
                  '进入相册',
                  style: TextStyle(
                    fontSize: 23,
                    fontWeight: FontWeight.normal,
                  ),
                ),
              );
            },
        ),
      ),
    );
  }
}

